package nl.myhealth.app.myhealthapp;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Set;

import nl.myhealth.app.myhealthapp.connection.ConnectThread;


public class DeviceActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{

    private ListView deviceListView;
    private TextView currentDeviceView;
    private Button cancelButton;
    private LinearLayout loader;

    private BluetoothAdapter btAdapter;
    private ArrayAdapter<String> listAdapter;

    private BluetoothDevice[] devices;

    private ConnectThread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device);

        deviceListView = (ListView)findViewById(R.id.activity_device_list);
        currentDeviceView = (TextView)findViewById(R.id.activity_device_current);
        cancelButton = (Button)findViewById(R.id.activity_device_cancel_button);
        loader = (LinearLayout)findViewById(R.id.activity_device_loader);

        btAdapter = BluetoothAdapter.getDefaultAdapter();
        listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        deviceListView.setOnItemClickListener(this);

        deviceListView.setAdapter(listAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!btAdapter.isEnabled())
        {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 100);
        }
        else
            initView();

    }

    private void initView()
    {
        if(MyHealthApplication.socket != null)
            currentDeviceView.setText(MyHealthApplication.socket.getRemoteDevice().getName());

        if(thread == null)
        {
            deviceListView.setEnabled(true);
            deviceListView.setVisibility(View.VISIBLE);
            cancelButton.setVisibility(View.GONE);
            loader.setVisibility(View.GONE);
        }
        else{
            deviceListView.setEnabled(false);
            deviceListView.setVisibility(View.GONE);
            cancelButton.setVisibility(View.VISIBLE);
            loader.setVisibility(View.VISIBLE);
        }

        devices = btAdapter.getBondedDevices().toArray(new BluetoothDevice[0]);

        listAdapter.clear();
        for(BluetoothDevice d : devices)
            listAdapter.add(d.getName());

        listAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100)
        {
            if(resultCode == RESULT_OK)
                initView();
            else
                finish();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.main_menu_dashboard) {
            Intent dashBoardIntent = new Intent(this, DashboardActivity.class);
            startActivity(dashBoardIntent);
            return true;
        }
        if (id == R.id.main_menu_profile) {
            Intent profileIntent = new Intent(this,ProfileActivity.class);
            startActivity(profileIntent);
            return true;
        }
        if (id == R.id.main_menu_bills) {
            Intent billsIntent = new Intent(this, BillsActivity.class);
            startActivity(billsIntent);
            return true;
        }
        if (id == R.id.main_menu_pulse) {
            Intent pulseIntent = new Intent(this,PulseActivity.class);
            startActivity(pulseIntent);
            return true;
        }
        if (id == R.id.main_menu_blood_pressure) {
            Intent pressureIntent = new Intent(this,PressureActivity.class);
            startActivity(pressureIntent);
            return true;
        }
        if (id == R.id.main_menu_ecg) {
            Intent ecgIntent = new Intent(this,ECGActivity.class);
            startActivity(ecgIntent);
            return true;
        }
        if (id == R.id.main_menu_urine) {
            Intent urineIntent = new Intent(this,UrineActivity.class);
            startActivity(urineIntent);
            return true;
        }
        if (id == R.id.main_menu_device) {
            Intent deviceIntent = new Intent(this, DeviceActivity.class);
            startActivity(deviceIntent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void cancelConnecting(View view)
    {
        if(thread != null) {
            thread.cancel();
            thread = null;
            deviceListView.setEnabled(true);
            deviceListView.setVisibility(View.VISIBLE);
            cancelButton.setVisibility(View.GONE);
            loader.setVisibility(View.GONE);
            currentDeviceView.setText(R.string.device_current_default_text);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        //Toast.makeText(this,"Clicked: " + devices[position].getName(),Toast.LENGTH_SHORT).show();
        currentDeviceView.setText("Connecting...");
        deviceListView.setEnabled(false);
        deviceListView.setVisibility(View.GONE);
        cancelButton.setVisibility(View.VISIBLE);
        loader.setVisibility(View.VISIBLE);
        thread = new ConnectThread(this,btAdapter,devices[position]);
        thread.start();
    }
}
